<?php
/**
 * (logic)
 */
namespace App\Logic\Operation;

use App\Models\Basics\SysCountry;
use App\Models\Operation\TCTotal;
use App\Models\Operation\TCTotalDetail;
use App\Models\Product\ProductClassify;
use App\Models\Operation\TCTotalFile;
class TCTFile
{
    public static function handle($start,$end,$countryId,$classifyId)
    {
        //运营总表核验
        $totals = TCTotal::query()
            ->where('report_date','>=',$start)
            ->where('report_date','<=',$end)
            ->where('country_id',$countryId)
            ->where('classify_id',$classifyId)
            ->get();
        $file = TCTotalFile::getFile($start,$end,$countryId,$classifyId);
        $fp = fopen($file, 'a');
        fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF));
        fputcsv($fp, ['开始日期','截至日期','国家','品类','广告费用(RMB)','返款金额(RMB)','促销费(RMB)','毛利(RMB)-扣减费用后']);
        $sum = self::_getSum($start,$end,$countryId,$classifyId);
        fputcsv($fp, $sum);
        fputcsv($fp, ['日期','汇率','SKU_ID','SKU_CODE',
            '销售额','销量','采购单价','总采购成本','平均运费','总运费','Fba费',
            '总Fba费','Commission','Commission费','退货率','退货费','VAT','VAT费','库存费','其它费','总成本','毛利','毛利(RMB)']);
        fputcsv($fp, ['说明','','','',
            '','','','采购单价*销量','','平均运费*销量','',
            'Fba费*销量','','Commission*销售额','','退货率*销售额','','VAT*销售额','1*销售额/100','1*销售额/100',
            '总采购成本+总运费+Fba费+Commission费+退货费+VAT费+库存费+其它费','销售额-总成本','']);
        foreach ($totals as $total){
            //运营总表明细
            $details = TCTotalDetail::query()->where('tc_id',$total->id)->get();
            foreach ($details as $detail){
                $temp = [];
                $temp['report_date'] = $total->report_date;
                $temp['rate_odds'] = $total->rate_odds;
                $temp['psku_id'] = $detail->psku_id;
                $temp['psku_code'] = $detail->sku->psku_code ?? '';
                $temp['amount'] = $detail->amount;
                $volume = $detail->volume;
                $temp['volume'] = $detail->volume;
                $tpl = round($detail->tpl_rmb / $total->rate_odds,3);
                $temp['tpl'] = $tpl;
                $temp['t_tpl'] = $volume * $tpl;
                $temp['head_cost'] = $detail->head_cost;
                $temp['t_head_cost'] = $volume * $detail->head_cost;
                $temp['t_fba_fee'] = $detail->fba_fee;
                $temp['fba_fee'] = $volume * $detail->fba_fee;
                $temp['commission'] = $detail->commission .'%';
                $temp['commission_fee'] = round($detail->amount * $detail->commission / 100 ,3);
                $temp['return'] = $detail->return .'%';
                $temp['return_fee'] = round($detail->amount * $detail->return / 100 ,3);
                $temp['vat'] = $detail->vat .'%';
                $temp['vat_fee'] = round($detail->amount * $detail->vat / 100 ,3);
                $temp['i_fee'] = round($detail->amount / 100 ,3);
                $temp['o_fee'] = round($detail->amount / 100 ,3);
                $temp['cost'] = $detail->cost;
                $temp['profit'] = $detail->profit;
                $temp['profit_rmb'] = round($detail->profit * $total->rate_odds,3);
                fputcsv($fp, $temp);
            }
        }
        fclose($fp);
        if(is_file($file)){
            //运营总表核验文件
            TCTotalFile::storeFile($start,$end,$countryId,$classifyId);
        }
    }

    private static function _getSum($start,$end,$countryId,$classifyId){
        $re = TCTotal::query()
            ->where('report_date','>=',$start)
            ->where('report_date','<=',$end)
            ->where('country_id',$countryId)
            ->where('classify_id',$classifyId)
            ->selectRaw('sum(spend) as spend,sum(reimburse) as reimburse,sum(promotion_discount) as promotion_discount,sum(profit) as profit')
            ->first();
        $country = SysCountry::getCountryById($countryId);
        $title = ProductClassify::getTitleById($classifyId);
        return [$start,$end,$country,$title,$re->spend ?? 0,$re->reimburse ?? 0,$re->promotion_discount ?? 0,$re->profit ?? 0];
    }
}
